package com.dx.mes.service.process.service;

import static com.dx.mes.common.bean.def.GenericDef.BANK_IN;
import static com.dx.mes.common.bean.def.GenericDef.EVT_LGOF;
import static com.dx.mes.common.bean.def.GenericDef.EVT_LGON;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_OPE;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_PATH;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_PATHI;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_TOOL_GR;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEIN_SHT_STATUS_INVALIED;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEOUT_INVALID_BNK_FLG;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEOUT_INVALID_INPUT;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEOUT_INVALID_PATH_CATE;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEOUT_NO_PRD_OUT_INFO;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEOUT_PATH_STATUS_INVALIED;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEOUT_SHT_STATUS_INVALIED;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEOUT_TOOL_UNMATCH;
import static com.dx.mes.common.bean.def.GenericDef.E_READ_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.E_RET_PRD_INFO;
import static com.dx.mes.common.bean.def.GenericDef.PATH_IS_FINISH;
import static com.dx.mes.common.bean.def.GenericDef.PATH_TYPE_REWK;
import static com.dx.mes.common.bean.def.GenericDef.PROC_HOLD;
import static com.dx.mes.common.bean.def.GenericDef.PROC_IN_PROCESS;
import static com.dx.mes.common.bean.def.GenericDef.PROC_WAIT;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_MPPREWORK;
import static com.dx.mes.common.bean.def.GenericDef._NORMAL;
import static com.dx.mes.common.bean.def.GenericDef._SPACE;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Bis_ope;
import com.dx.common.table.ppt.Bis_opeId;
import com.dx.common.table.ppt.Bis_path;
import com.dx.common.table.ppt.Bis_pathId;
import com.dx.common.table.ppt.Bis_pathi;
import com.dx.common.table.ppt.Bis_pathiId;
import com.dx.common.table.ppt.Bis_tool;
import com.dx.common.table.ppt.Bis_tool_gr;
import com.dx.common.table.ppt.Bis_tool_grId;
import com.dx.common.table.ppt.Ret_line_wo;
import com.dx.common.table.ppt.Ret_prd_info;
import com.dx.common.table.ppt.Ret_prd_jge;
import com.dx.common.table.ppt.Ret_prd_jgeId;
import com.dx.common.table.ppt.Ret_prd_out;
import com.dx.mes.common.dao.IBisOpeDao;
import com.dx.mes.common.dao.IBisPathDao;
import com.dx.mes.common.dao.IBisPathiDao;
import com.dx.mes.common.dao.IBisToolDao;
import com.dx.mes.common.dao.IBisToolGrDao;
import com.dx.mes.common.dao.IRetLineWoDao;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.dao.IRetPrdJgeDao;
import com.dx.mes.common.dao.IRetPrdOutDao;
import com.dx.mes.common.table.rtnBean.RouteObj;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.GUIDGenerator;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.process.service.IMppreworkService;
import com.dx.mes.facade.process.tx.Mpprework.MppreworkI;
import com.dx.mes.facade.process.tx.Mpprework.MppreworkIA;
import com.dx.mes.facade.process.tx.Mpprework.MppreworkO;

/**
 * Created by admin on 2017/12/28.
 */
@Service("mppreworkService")
public class MppreworkService implements IMppreworkService{
    private static final String TX_NAME = T_MPPREWORK;
    private LogUtils logUtils;

    @Autowired
    private IRetPrdInfoDao retPrdInfoDao;
    @Autowired
    private IBisOpeDao bisOpeDao;
    @Autowired
    private IBisToolGrDao bisToolGrDao;
    @Autowired
    private IBisPathiDao bisPathiDao;
    @Autowired
    private IBisPathDao bisPathDao;
    @Autowired
    private IRetPrdJgeDao retPrdJgeDao;
    @Autowired
    private IRetPrdOutDao retPrdOutDao;
    @Autowired
    private IRetLineWoDao retLineWoDao;
    @Autowired
    private IBisToolDao bisToolDao;

    @Override
    public String subMainProc(String evt_no, String strInTrx) {
        AppContext.clear();
        AppContext.setCurrServiceName(MppreworkService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(MppreworkService.class);
        logUtils.info("[InTrx:" + strInTrx + "]");

        Session session = retPrdInfoDao.getCrSession();
        MppreworkO outTrx = new MppreworkO();
        String strOutTrx = null;

        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        // 输入对象
        try {
            MppreworkI inTrx = new MppreworkI();
            // 将json字符串转换成对象
            inTrx = JacksonUtil.fromJson(strInTrx, MppreworkI.class);
            char action_flg = inTrx.getAction_flg().charAt(0);

            switch (action_flg) {
                case 'M':
                    moveOutForReWork(inTrx, outTrx);
                    break;
                default:
                    errActionFnc(outTrx);
            }
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
                session.flush();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception ex) {
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            outTrx.setRtn_mesg(ex.toString());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            // 将一个 Object 或者List对象转化为JSOObject或者JSONArray
        }
        strOutTrx = JacksonUtil.toJSONStr(outTrx);
        logUtils.info("[OutTrx:" + strOutTrx + "]");
        return strOutTrx;
    }
    public void moveOutForReWork(MppreworkI inTrx, MppreworkO outTrx) {

        SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
        if(inTrx.getIary().size() == 0){
            outTrx.setRtn_code(E_FPPMOVEOUT_INVALID_INPUT + "");
            outTrx.setRtn_mesg("入账信息不正确");
            return;
        }
        MppreworkIA iary = inTrx.getIary().get(0);
        if(iary !=null ){
            String prdSeqId =  iary.getPrd_seq_id();
            Ret_prd_info ret_prd_info = retPrdInfoDao.getWithLock(iary.getPrd_seq_id(), LockMode.UPGRADE);
            if (ret_prd_info == null) {
                outTrx.setRtn_code(E_RET_PRD_INFO + E_READ_NOT_FOUND + "");
                outTrx.setRtn_mesg("屏体信息不存在");
                return;
            }
            //检查工艺路线是否HOLD
            Bis_pathiId bis_pathiId = new Bis_pathiId();
            bis_pathiId.setPath_id(ret_prd_info.getCr_path_id_fk());
            bis_pathiId.setPath_ver(ret_prd_info.getCr_path_ver_fk());
            Bis_pathi bis_pathi = bisPathiDao.get(bis_pathiId);
            if(bis_pathi == null){
                outTrx.setRtn_code(E_BIS_PATHI + E_READ_NOT_FOUND + "");
                outTrx.setRtn_mesg("未找到屏体工艺路线信息" + bis_pathiId.toString());
                return;
            }else if(PROC_HOLD.equals(bis_pathi.getPath_stat())){
                outTrx.setRtn_code(E_FPPMOVEOUT_PATH_STATUS_INVALIED + "");
                outTrx.setRtn_mesg("屏体工艺路线" + bis_pathiId.toString() + "状态为[" + PROC_HOLD + "]");
                return;
            }else if(!PATH_TYPE_REWK.equals(bis_pathi.getPath_cate())){
                outTrx.setRtn_code(E_FPPMOVEOUT_INVALID_PATH_CATE + "");
                outTrx.setRtn_mesg("屏体工艺路线" + bis_pathiId.toString() + "必须为重工工艺路线[" + PATH_TYPE_REWK + "]");
                return;
            }
            //出账
            rtnBeen = realMoveOutProcForRework(inTrx, ret_prd_info);
            if (rtnBeen.getlRc() != _NORMAL) {
                outTrx.setRtn_code(rtnBeen.getlRc() + "");
                outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
                return;
            }
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }
    private SimpleRtnBeen checkPrdInfo(MppreworkIA iary, Ret_prd_info ret_prd_info) {
        Bis_opeId bis_opeId = new Bis_opeId();
        if (!PROC_IN_PROCESS.equals(ret_prd_info.getPrd_stat())){
            return new SimpleRtnBeen(E_FPPMOVEOUT_SHT_STATUS_INVALIED, "屏体状态不正确，必须为[" + PROC_IN_PROCESS + "]请确认");
        }else{
            bis_opeId.setOpe_id(ret_prd_info.getCr_ope_id_fk());
            bis_opeId.setOpe_ver(ret_prd_info.getCr_ope_ver_fk());
        }

        //获取站点信息
        Bis_ope bis_ope = bisOpeDao.get(bis_opeId);
        if(bis_ope == null){
            return new SimpleRtnBeen(E_BIS_OPE + E_READ_NOT_FOUND, "未找到屏体站点信息" + bis_opeId.toString());
        }
        //获取设备群组信息
        Bis_tool_grId bis_tool_grId = new Bis_tool_grId();
        bis_tool_grId.setTool_id_fk(iary.getTool_id_fk());
        bis_tool_grId.setToolg_id(bis_ope.getToolg_id_fk());
        Bis_tool_gr bis_tool_gr = bisToolGrDao.get(bis_tool_grId);
        if(bis_tool_gr == null){
            return new SimpleRtnBeen(E_BIS_TOOL_GR + E_READ_NOT_FOUND, "屏体站点" + bis_opeId.toString()
                    + "对应设备组[" + bis_ope.getToolg_id_fk()
                    + "]和入账设备[" + iary.getTool_id_fk() + "]无关联，无法出账，请确认");
        }

        //检查入账设备和出账设备是否相同
        if(!iary.getTool_id_fk().equals(ret_prd_info.getCr_tool_id_fk())){
            return new SimpleRtnBeen(E_FPPMOVEOUT_TOOL_UNMATCH, "屏体出账设备[" + iary.getTool_id_fk()
                    + "]与入账设备[" + ret_prd_info.getCr_tool_id_fk() + "]不匹配,请确认");
        }

        //在制仓检查
        if (BANK_IN.equals(ret_prd_info.getBnk_flg())) {
            return new SimpleRtnBeen(E_FPPMOVEOUT_INVALID_BNK_FLG, "屏体当前在在制品仓，请先从在制品仓转出");
        }

        return new SimpleRtnBeen(_NORMAL);
    }
    private SimpleRtnBeen realMoveOutProcForRework(MppreworkI inTrx, Ret_prd_info ret_prd_info) {

        String repairFlg = inTrx.getRepair_flg();
        MppreworkIA iary = inTrx.getIary().get(0);
        Timestamp current_timestamp = Util.getCurrentTimestamp();
        //出账时清空pv信息
        ret_prd_info.setPv_ope_id_fk(_SPACE);
        ret_prd_info.setPv_ope_no_fk(_SPACE);
        ret_prd_info.setPv_ope_ver_fk(_SPACE);
        ret_prd_info.setPv_path_id_fk(_SPACE);
        ret_prd_info.setPv_path_ver_fk(_SPACE);
        ret_prd_info.setPv_tool_id_fk(_SPACE);
        ret_prd_info.setPv_toolg_id_fk(_SPACE);
        ret_prd_info.setPv_proc_id_fk(_SPACE);
        ret_prd_info.setPv_tool_run_mode(_SPACE);

        if(!Util.isSpaceCheck(iary.getPrd_grade())){
            ret_prd_info.setPrd_grade(iary.getPrd_grade());
        }
        if(!Util.isSpaceCheck(iary.getDefect_code())){
            ret_prd_info.setRsn_code(iary.getDefect_code());
            Ret_prd_jgeId ret_prd_jgeId = new Ret_prd_jgeId();
            ret_prd_jgeId.setPrd_seq_id_fk(ret_prd_info.getPrd_seq_id());
            ret_prd_jgeId.setEvt_seq_id_fk(GUIDGenerator.javaGUID());
            ret_prd_jgeId.setData_seq_id_fk(1);
            Ret_prd_jge ret_prd_jge = new Ret_prd_jge();
            ret_prd_jge.setId(ret_prd_jgeId);
            ret_prd_jge.setPath_id_fk(ret_prd_info.getCr_path_id_fk());
            ret_prd_jge.setPath_ver_fk(ret_prd_info.getCr_path_ver_fk());
            ret_prd_jge.setOpe_no_fk(ret_prd_info.getCr_ope_no_fk());
            ret_prd_jge.setOpe_id_fk(ret_prd_info.getCr_ope_id_fk());
            ret_prd_jge.setOpe_ver_fk(ret_prd_info.getCr_ope_ver_fk());
            ret_prd_jge.setProc_id(ret_prd_info.getCr_proc_id_fk());
            ret_prd_jge.setPrd_grade_fk(ret_prd_info.getPrd_grade());
            ret_prd_jge.setJudge_cnt(1);
            ret_prd_jge.setJudge_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
            ret_prd_jge.setJudge_usr(inTrx.getEvt_usr());
            ret_prd_jge.setJudge_timestamp(current_timestamp);
            ret_prd_jge.setDef_code(iary.getDefect_code());
            ret_prd_jge.setEvt_timestamp(current_timestamp);
            ret_prd_jge.setEvt_usr(inTrx.getEvt_usr());
            retPrdJgeDao.save(ret_prd_jge);
        }
        if("O".equals(repairFlg)){//判至到途程下一个站点 ，
            if(Util.isSpaceCheck(ret_prd_info.getNx_ope_no_fk())){//如果是途程最后一站
                if("$FMA".equals(ret_prd_info.getCr_toolg_id_fk())){//最后一站是不是FMA，如果是，下一站点信息则赋值为转出站点
                    List<Ret_prd_out> ret_prd_out_list = retPrdOutDao.find("FROM Ret_prd_out WHERE prd_seq_id_fk = '"
                            + ret_prd_info.getPrd_seq_id() + "' ORDER BY evt_timestamp asc");
                    if(ret_prd_out_list.isEmpty()){
                        return new SimpleRtnBeen(E_FPPMOVEOUT_NO_PRD_OUT_INFO, "未找到屏体" + ret_prd_info.getPrd_seq_id() + "转出信息[RET_PRD_OUT]");
                    }else{
                        Ret_prd_out ret_prd_out = ret_prd_out_list.get(0);
                        Bis_pathId bis_pathId = new Bis_pathId();
                        bis_pathId.setPath_id_fk(ret_prd_out.getOut_path_id_fk());
                        bis_pathId.setPath_ver_fk(ret_prd_out.getOut_path_ver_fk());
                        bis_pathId.setCr_ope_no(ret_prd_out.getOut_ope_no_fk());
                        Bis_path bis_path = bisPathDao.get(bis_pathId);
                        if(bis_path == null){
                            return new SimpleRtnBeen(E_BIS_PATH + E_READ_NOT_FOUND, "未找到工艺路线设定站点信息" + bis_pathId.toString());
                        }
                        //获取站点信息
                        Bis_opeId bis_opeId = new Bis_opeId();
                        bis_opeId.setOpe_id(bis_path.getCr_ope_id_fk());
                        bis_opeId.setOpe_ver(bis_path.getCr_ope_ver_fk());
                        Bis_ope bis_ope = bisOpeDao.get(bis_opeId);
                        if(bis_ope == null){
                            return new SimpleRtnBeen(E_BIS_OPE + E_READ_NOT_FOUND, "未找到站点信息" + bis_opeId.toString());
                        }
                        //Set Ret_prd_info
                        ret_prd_info.setNx_ope_no_fk(bis_path.getId().getCr_ope_no());
                        ret_prd_info.setNx_ope_id_fk(bis_path.getCr_ope_id_fk());
                        ret_prd_info.setNx_ope_ver_fk(bis_path.getCr_ope_ver_fk());
                        ret_prd_info.setNx_path_id_fk(bis_path.getId().getPath_id_fk());
                        ret_prd_info.setNx_path_ver_fk(bis_path.getId().getPath_ver_fk());
                        ret_prd_info.setNx_proc_id_fk(bis_ope.getProc_id());
                        ret_prd_info.setNx_toolg_id_fk(bis_ope.getToolg_id_fk());
                        ret_prd_info.setNx_tool_run_mode(bis_ope.getTool_run_mode());
                    }
                    //删除Ret_prd_out
                    retPrdOutDao.deleteAll(ret_prd_out_list);
                }else{//如果不是，下一站点信息则赋值为FMA站点
                    String hqlM ="From Bis_path where path_id_fk='REWFMA01' and path_ver_fk='00000'";//FMA站点的路线只有一条，具体定义，需修改
                    List<Bis_path> bis_paths = bisPathDao.find(hqlM);
                    if(bis_paths.size()>0){
                        Bis_path bis_path = bis_paths.get(0);
                        Bis_opeId bis_opeId = new Bis_opeId();
                        bis_opeId.setOpe_id(bis_path.getCr_ope_id_fk());
                        bis_opeId.setOpe_ver(bis_path.getCr_ope_ver_fk());
                        Bis_ope bis_ope = bisOpeDao.get(bis_opeId);

                        //Set Ret_prd_info
                        ret_prd_info.setNx_ope_no_fk(bis_path.getId().getCr_ope_no());
                        ret_prd_info.setNx_ope_id_fk(bis_path.getCr_ope_id_fk());
                        ret_prd_info.setNx_ope_ver_fk(bis_path.getCr_ope_ver_fk());
                        ret_prd_info.setNx_path_id_fk(bis_path.getId().getPath_id_fk());
                        ret_prd_info.setNx_path_ver_fk(bis_path.getId().getPath_ver_fk());
                        ret_prd_info.setNx_proc_id_fk(bis_ope.getProc_id());
                        ret_prd_info.setNx_toolg_id_fk(bis_ope.getToolg_id_fk());
                        ret_prd_info.setNx_tool_run_mode(bis_ope.getTool_run_mode());
                    }
                }
            }
            //不是最后一站则只需修改状态，下一站点信息入账时已经赋值
            ret_prd_info.setPrd_stat(PROC_WAIT);
            ret_prd_info.setSht_ope_msg(iary.getSht_ope_msg());
            ret_prd_info.setLogof_usr(inTrx.getEvt_usr());
            ret_prd_info.setLogof_timestamp(current_timestamp);
            ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
            ret_prd_info.setEvt_cate(EVT_LGOF);
            ret_prd_info.setEvt_timestamp(current_timestamp);
            retPrdInfoDao.update(ret_prd_info);
            //检查是否需要跳站和自动入账功能
            SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
            rtnBeen =  skipFunc(inTrx, ret_prd_info);
            if (rtnBeen.getlRc() != _NORMAL) {
                return new SimpleRtnBeen(rtnBeen.getlRc() ,
                        rtnBeen.getMsgBuf());
            }
        }else{//判至到FMA
            String hqlM ="From Bis_path where path_id_fk='REWFMA01' and path_ver_fk='00000'";//FMA站点的路线只有一条，具体定义，需修改
            List<Bis_path> bis_paths = bisPathDao.find(hqlM);
            if(bis_paths.size()>0) {
                Bis_path bis_path = bis_paths.get(0);
                Bis_opeId bis_opeId = new Bis_opeId();
                bis_opeId.setOpe_id(bis_path.getCr_ope_id_fk());
                bis_opeId.setOpe_ver(bis_path.getCr_ope_ver_fk());
                Bis_ope bis_ope = bisOpeDao.get(bis_opeId);

                //Set Ret_prd_info
                ret_prd_info.setNx_ope_no_fk(bis_path.getId().getCr_ope_no());
                ret_prd_info.setNx_ope_id_fk(bis_path.getCr_ope_id_fk());
                ret_prd_info.setNx_ope_ver_fk(bis_path.getCr_ope_ver_fk());
                ret_prd_info.setNx_path_id_fk(bis_path.getId().getPath_id_fk());
                ret_prd_info.setNx_path_ver_fk(bis_path.getId().getPath_ver_fk());
                ret_prd_info.setNx_proc_id_fk(bis_ope.getProc_id());
                ret_prd_info.setNx_toolg_id_fk(bis_ope.getToolg_id_fk());
                ret_prd_info.setNx_tool_run_mode(bis_ope.getTool_run_mode());
                ret_prd_info.setPrd_stat(PROC_WAIT);
                ret_prd_info.setSht_ope_msg(iary.getSht_ope_msg());
                ret_prd_info.setLogof_usr(inTrx.getEvt_usr());
                ret_prd_info.setPv_logof_timestamp(ret_prd_info.getLogof_timestamp());
                ret_prd_info.setLogof_timestamp(current_timestamp);
                ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
                ret_prd_info.setEvt_cate(EVT_LGOF);
                ret_prd_info.setEvt_timestamp(current_timestamp);
                retPrdInfoDao.update(ret_prd_info);
            }
        }
        return new SimpleRtnBeen(_NORMAL);
    }
    private SimpleRtnBeen skipFunc(MppreworkI inTrx, Ret_prd_info ret_prd_info) {
        Timestamp evt_timestamp = Util.getCurrentTimestamp();
        String    evt_usr = inTrx.getEvt_usr();
        Bis_pathId bis_pathIdS = new Bis_pathId();
        bis_pathIdS.setPath_id_fk(ret_prd_info.getNx_path_id_fk());
        bis_pathIdS.setPath_ver_fk(ret_prd_info.getNx_path_ver_fk());
        bis_pathIdS.setCr_ope_no(ret_prd_info.getNx_ope_no_fk());
        Bis_path bis_pathS = bisPathDao.get(bis_pathIdS);
        if (null == bis_pathS) {
            return new SimpleRtnBeen(E_FPPMOVEIN_SHT_STATUS_INVALIED ,"途程站点信息不存在" );
        }
        if (!Util.isSpaceCheck(bis_pathS.getSkip_flg())) {
            if (bis_pathS.getSkip_flg().equals("1")) {
                retPrdInfoDao.update(ret_prd_info);
                Bis_opeId bis_opeId1 = new Bis_opeId();
                bis_opeId1.setOpe_id(ret_prd_info.getNx_ope_id_fk());
                bis_opeId1.setOpe_ver(ret_prd_info.getNx_ope_ver_fk());
                Bis_ope bis_ope1 = bisOpeDao.get(bis_opeId1);
                if (null == bis_ope1) {
                    return new SimpleRtnBeen(E_FPPMOVEIN_SHT_STATUS_INVALIED  ,
                            "站点[" + ret_prd_info.getNx_ope_id_fk() + "]不存在" );
                }
                String hqltool1 = "FROM Bis_tool_gr where 1=1 and toolg_id='" + bis_ope1.getToolg_id_fk() + "'";
                List<Bis_tool_gr> bisToolGr1 = bisToolGrDao.find(hqltool1);
                String woIdFk = ret_prd_info.getWo_id_fk();
                String linwosql = "FROM Ret_line_wo where 1=1";
                linwosql += " and wo_id_fk = '" + woIdFk + "'";
                linwosql += " and ope_id_fk = '" + ret_prd_info.getNx_ope_id_fk() + "'";
                linwosql += " and valid_flg = 'Y'";
                List<Ret_line_wo> retLineWoList = retLineWoDao.find(linwosql);
                if(retLineWoList != null && retLineWoList.size() > 0){
                    //所有设备组下的设备ID
                    List<String> toolIdList = new ArrayList<>();
                    if (bisToolGr1.size() != 0) {
                        for(int j = 0;j < bisToolGr1.size();j++){
                            toolIdList.add(bisToolGr1.get(j).getId().getTool_id_fk());
                        }
                    }

                    //对应线别下的设备(从设备组中筛选）
                    String lineIdFk = retLineWoList.get(0).getLine_id_fk();
                    String hql1 = "FROM Bis_tool where 1=1";
                    hql1 += " and bay_id = '" +lineIdFk+"'";
                    String str = "";
                    for (int i = 0; i < toolIdList.size() - 1; i++) {
                        str = str + "'" + toolIdList.get(i).toString().trim() + "',";
                    }
                    str = str + "'" + toolIdList.get(toolIdList.size() - 1).toString().trim() + "'";
                    if (toolIdList.size() > 0) {
                        hql1 = hql1 + " and tool_id  in(" + str + ")";
                    }
                    List<Bis_tool> bisToolList = bisToolDao.find(hql1);

                    if(bisToolList != null && bisToolList.size() > 0){
                        ret_prd_info.setCr_tool_id_fk(bisToolList.get(0).getTool_id());
                    }
                    //存储pv信息
                    ret_prd_info.setPv_ope_id_fk(ret_prd_info.getCr_ope_id_fk());
                    ret_prd_info.setPv_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
                    ret_prd_info.setPv_ope_ver_fk(ret_prd_info.getCr_ope_ver_fk());
                    ret_prd_info.setPv_path_id_fk(ret_prd_info.getCr_path_id_fk());
                    ret_prd_info.setPv_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
                    ret_prd_info.setPv_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
                    ret_prd_info.setPv_toolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
                    ret_prd_info.setPv_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
                    ret_prd_info.setPv_tool_run_mode(ret_prd_info.getTool_port_id_fk());

                    //存储cr信息
                    ret_prd_info.setCr_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
                    ret_prd_info.setCr_ope_no_fk(ret_prd_info.getNx_ope_no_fk());
                    ret_prd_info.setCr_ope_ver_fk(ret_prd_info.getNx_ope_ver_fk());
                    ret_prd_info.setCr_path_id_fk(ret_prd_info.getNx_path_id_fk());
                    ret_prd_info.setCr_path_ver_fk(ret_prd_info.getNx_path_ver_fk());
                    ret_prd_info.setCr_toolg_id_fk(ret_prd_info.getNx_toolg_id_fk());
                    ret_prd_info.setCr_proc_id_fk(ret_prd_info.getNx_proc_id_fk());
                    ret_prd_info.setCr_tool_run_mode(ret_prd_info.getNx_tool_run_mode());

                    //获取下一制程站点信息
                    RouteObj nxRouteObj = bisPathDao.getNxPath(ret_prd_info.getNx_path_id_fk(),
                            ret_prd_info.getNx_path_ver_fk(), ret_prd_info.getNx_ope_no_fk());
                    if (_NORMAL == nxRouteObj.getLRc()) {
                        logUtils.info("[login_normal:" + ret_prd_info.getPrd_seq_id() + "]");
                        Bis_path nx_bis_path1 = nxRouteObj.getPath();
                        Bis_ope nx_bis_ope1 = nxRouteObj.getOpe();
                        ret_prd_info.setNx_ope_no_fk(nx_bis_path1.getId().getCr_ope_no());
                        ret_prd_info.setNx_ope_id_fk(nx_bis_path1.getCr_ope_id_fk());
                        ret_prd_info.setNx_ope_ver_fk(nx_bis_path1.getCr_ope_ver_fk());
                        ret_prd_info.setNx_path_id_fk(nx_bis_path1.getId().getPath_id_fk());
                        ret_prd_info.setNx_path_ver_fk(nx_bis_path1.getId().getPath_ver_fk());
                        ret_prd_info.setNx_proc_id_fk(nx_bis_ope1.getProc_id());
                        ret_prd_info.setNx_toolg_id_fk(nx_bis_ope1.getToolg_id_fk());
                        ret_prd_info.setNx_tool_run_mode(nx_bis_ope1.getTool_run_mode());
                    } else if (PATH_IS_FINISH == nxRouteObj.getLRc()) {
                        logUtils.info("[login_finish:" + ret_prd_info.getPrd_seq_id() + "]");
                        ret_prd_info.setNx_toolg_id_fk(_SPACE);
                        ret_prd_info.setNx_ope_id_fk(_SPACE);
                        ret_prd_info.setNx_ope_no_fk(_SPACE);
                        ret_prd_info.setNx_ope_ver_fk(_SPACE);
                        ret_prd_info.setNx_path_id_fk(_SPACE);
                        ret_prd_info.setNx_path_ver_fk(_SPACE);
                        ret_prd_info.setNx_proc_id_fk(_SPACE);
                        ret_prd_info.setNx_tool_run_mode(_SPACE);
                    } else {
                        return new SimpleRtnBeen(E_BIS_OPE + E_READ_NOT_FOUND ,
                                "获取下一制程站点错误：" + nxRouteObj.getGszMsgBuf());
                    }
                    ret_prd_info.setLogon_usr(evt_usr);
                    ret_prd_info.setPv_logof_timestamp(ret_prd_info.getLogof_timestamp());
                    ret_prd_info.setLogon_timestamp(evt_timestamp);
                    ret_prd_info.setPrd_stat(PROC_IN_PROCESS);
                    ret_prd_info.setEvt_usr(evt_usr);
                    ret_prd_info.setEvt_cate(EVT_LGON);
                    ret_prd_info.setEvt_timestamp(evt_timestamp);
                }else{
                    return new SimpleRtnBeen(E_BIS_OPE + E_READ_NOT_FOUND ,
                            "当前工单未绑定或激活线别与站点，无法自动入账！");
                }
            }else if (bis_pathS.getSkip_flg().equals("3")) {//跳站
                //获取下一制程站点信息
                RouteObj nxRouteObj = bisPathDao.getNxPath(ret_prd_info.getNx_path_id_fk(),
                        ret_prd_info.getNx_path_ver_fk(), ret_prd_info.getNx_ope_no_fk());
                if (_NORMAL == nxRouteObj.getLRc()) {
                    logUtils.info("[login_normal:" + ret_prd_info.getPrd_seq_id() + "]");
                    Bis_path nx_bis_path1 = nxRouteObj.getPath();
                    Bis_ope nx_bis_ope1 = nxRouteObj.getOpe();
                    ret_prd_info.setNx_ope_no_fk(nx_bis_path1.getId().getCr_ope_no());
                    ret_prd_info.setNx_ope_id_fk(nx_bis_path1.getCr_ope_id_fk());
                    ret_prd_info.setNx_ope_ver_fk(nx_bis_path1.getCr_ope_ver_fk());
                    ret_prd_info.setNx_path_id_fk(nx_bis_path1.getId().getPath_id_fk());
                    ret_prd_info.setNx_path_ver_fk(nx_bis_path1.getId().getPath_ver_fk());
                    ret_prd_info.setNx_proc_id_fk(nx_bis_ope1.getProc_id());
                    ret_prd_info.setNx_toolg_id_fk(nx_bis_ope1.getToolg_id_fk());
                    ret_prd_info.setNx_tool_run_mode(nx_bis_ope1.getTool_run_mode());
                } else if (PATH_IS_FINISH == nxRouteObj.getLRc()) {
                    logUtils.info("[login_finish:" + ret_prd_info.getPrd_seq_id() + "]");
                    ret_prd_info.setNx_toolg_id_fk(_SPACE);
                    ret_prd_info.setNx_ope_id_fk(_SPACE);
                    ret_prd_info.setNx_ope_no_fk(_SPACE);
                    ret_prd_info.setNx_ope_ver_fk(_SPACE);
                    ret_prd_info.setNx_path_id_fk(_SPACE);
                    ret_prd_info.setNx_path_ver_fk(_SPACE);
                    ret_prd_info.setNx_proc_id_fk(_SPACE);
                    ret_prd_info.setNx_tool_run_mode(_SPACE);
                }
                else {
                    return new SimpleRtnBeen(E_BIS_OPE + E_READ_NOT_FOUND ,
                            "获取下一制程站点错误：" + nxRouteObj.getGszMsgBuf());
                }
            }else{
                return new SimpleRtnBeen(_NORMAL);
            }
            this.skipFunc(inTrx, ret_prd_info);//可能出现连续跳站的情况，所以递归循环
        }
        return new SimpleRtnBeen(_NORMAL);
    }
    public void errActionFnc(MppreworkO outTrx) {
        outTrx.setRtn_code("0000009");
        outTrx.setRtn_mesg("Invalid Action_flg");
    }

}
